Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tray_icon crate for better tray icon support #288

Merged
merged 16 commits into from
Sep 2, 2024
Merged

Conversation

dastansam
Copy link
Contributor

@dastansam dastansam commented Aug 12, 2024

closes #222

tested on MacOS

Screen.Recording.2024-08-12.at.10.49.15.mov

Copy link
Member

@nazar-pc nazar-pc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for converting it! Didn't know you were working on it, ping me in such cases so we don't do the same work twice (I didn't start on this yet, don't worry).

Looks like a much nicer library than betrayer, wondering why it wasn't used in the first place 🤔

This will need to be tested on Windows and Linux, you'll also have to update documentation on required dependencies since it seems like we have some added in here. For Linux we should be using libayatana-appindicator3-dev as a dependency.

One thing that will likely make this unusable/unacceptable for Linux is that it is based on GTK3, not GTK4.
GTK3 is legacy and we don't compile/include it on Windows or macOS.
I think it'll work for Windows and macOS since it is not GTK3 that is used there to drive tray icon (hence the question why atk was installed on macOS).
On Linux https://github.com/iovxw/ksni was suggested in tauri-apps/tray-icon#107 and probably worth exploring (betrayer for example doesn't work on my system, but ksni seems to support more protocols and should start working).

Assuming we go with 2 libraries, the tray icon creation should probably be wrapped into a platform-specific struct that will handle the initialization and that we can store in the App instance.

Overall this is a good start, but needs more work and more testing before we can merge it.

P.S. Tray icons are a mess on Linux:

Cargo.toml Outdated Show resolved Hide resolved
src/frontend.rs Outdated
Comment on lines 593 to 597
let image = image::load_from_memory(ICON)
.expect("Statically correct image; qed")
.to_rgba8();

let (width, height) = image.dimensions();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These variables will be unused on Windows and cause clippy warnings. They should be grouped with tray icon construction using let tray_icon = {};.

Also you can use image::load_from_memory_with_format(ICON, image::ImageFormat::Png) as more concrete method and you can use image.width() instead of extracting separate variables first.

src/frontend.rs Outdated Show resolved Hide resolved
src/frontend.rs Outdated
.with_menu(std::boxed::Box::new(
Menu::with_items(&[
&MenuItem::with_id(
TRAY_ICON_MENU_OPEN_ID,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you don't really need to assign ID explicitly, just create menu item with ::new() and then store ID in a variable with menu_item.id() call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tray icon initialization and receiving menu events are in different scopes with the latest refactoring. do you think I should just save the IDs and expose a method to retrieve them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do. I basically expect something like platform-specific constructors or even functions that take Sender as an argument and return opaque TrayIcon instance back that frontend will need to keep around for tray icon to continue functioning.

src/frontend.rs Outdated
Menu::with_items(&[
&MenuItem::with_id(
TRAY_ICON_MENU_OPEN_ID,
T.tray_icon_open().to_string(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shoudn't need an allocation, try this:

Suggested change
T.tray_icon_open().to_string(),
&T.tray_icon_open(),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried this, but it raises a compile error due to trait bound of AsrRef<str>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be de-referenced first: &*T.tray_icon_open()

src/frontend.rs Outdated Show resolved Hide resolved
src/frontend.rs Outdated Show resolved Hide resolved
src/frontend.rs Outdated Show resolved Hide resolved
.github/workflows/rust.yml Outdated Show resolved Hide resolved
.github/workflows/rust.yml Outdated Show resolved Hide resolved
@dastansam dastansam force-pushed the replace-tray-icon branch 2 times, most recently from 2209bac to fbb7b7c Compare August 14, 2024 17:33
@dastansam dastansam requested a review from nazar-pc August 14, 2024 19:28
Copy link
Member

@nazar-pc nazar-pc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a step in the right direction, but a bit overengineered and Linux support is not implemented fully

.github/workflows/rust.yml Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
src/frontend.rs Outdated Show resolved Hide resolved
src/frontend.rs Outdated Show resolved Hide resolved
src/frontend.rs Outdated
Menu::with_items(&[
&MenuItem::with_id(
TRAY_ICON_MENU_OPEN_ID,
T.tray_icon_open().to_string(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be de-referenced first: &*T.tray_icon_open()

src/frontend.rs Outdated Show resolved Hide resolved
src/frontend.rs Outdated Show resolved Hide resolved
src/frontend.rs Outdated Show resolved Hide resolved
src/frontend.rs Outdated Show resolved Hide resolved
src/frontend.rs Outdated Show resolved Hide resolved
@dastansam dastansam requested a review from nazar-pc August 26, 2024 12:43
@dastansam
Copy link
Contributor Author

I haven't yet tested this on non-MacOS Linux machine but I think I resolved all the comments code-wise @nazar-pc

@nazar-pc
Copy link
Member

I did some refactoring, fixed some issues and added single left click handling. Tested on Windows and Ubuntu and confirmed that it works there at least.

@dastansam can you confirm it still works on macOS as expected?

@nazar-pc
Copy link
Member

Still doesn't work on my Linux due to lack of legacy icons support in the library, but it is not worse than before, so not a blocker.

@dastansam
Copy link
Contributor Author

dastansam commented Sep 1, 2024

I did some refactoring, fixed some issues and added single left click handling. Tested on Windows and Ubuntu and confirmed that it works there at least.

@dastansam can you confirm it still works on macOS as expected?

open button works fine, but close button quits the application. I thought it would just hide the window. Maybe then we can rename it Quit?

@nazar-pc
Copy link
Member

nazar-pc commented Sep 1, 2024

I thought it would just hide the window. Maybe then we can rename it Quit?

It closes the app. It was working like this until you changed it, so I restored it back to what it was. Renaming is possible, but we'll have to update a bunch of translations in that case. I'll take care of it tomorrow alongside with one more improvement.

@nazar-pc nazar-pc enabled auto-merge (squash) September 2, 2024 06:53
@nazar-pc nazar-pc merged commit 20f8ac3 into main Sep 2, 2024
8 checks passed
@nazar-pc nazar-pc deleted the replace-tray-icon branch September 2, 2024 07:14
clostao added a commit that referenced this pull request Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace tray icon implementation with a better one
2 participants